#!/usr/bin/env python

import logging
import urllib

def __create_object(name, *args):
   object_name = ""
   exec("object_name = %s" % name)
   return object_name(*args)

def create(config):
   name = config['source#type']
   children = config['source#children']
   args = [ ]
   for child in children:
      args.append(config[child])

   return __create_object(name, *args)

class picture_source:
   def get_picture(self):
      return None

class flip_flop_ps(picture_source):
   def __init__(self, picture1, picture2):
      logging.debug("FlipFlop Picture Source Initiated")
      self.pictures = [ open(picture1, "rb").read(),
                        open(picture2, "rb").read(),
                      ]
      self.switch = False

   def get_picture(self):
      if (self.switch):
         index = 0
      else:
         index = 1

      self.switch = not self.switch
      return self.pictures[index]

class url_ps(picture_source):
   def __init__(self, url):
      logging.debug("URL Picture Source Initiated")
      self.url = url

   def get_picture(self):
      return urllib.urlopen(self.url).read()
